package com.xing.binbackend.topology.service;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.xing.binbackend.topology.model.CloudService;
import com.xing.binbackend.topology.remote.CmdbCacheService;
import org.springframework.stereotype.Service;

import java.util.Map;

@Service
public class TopologyServiceImpl {

    private final ObjectMapper mapper;

    private final CmdbCacheService cmdbCacheService;

    public TopologyServiceImpl(ObjectMapper objectMapper, CmdbCacheService cmdbCacheService) {
        this.mapper = objectMapper;
        this.cmdbCacheService = cmdbCacheService;
    }

    /**
     * 加载顺序
     * 静态代码块 变量 仅执行1次
     * 实实例变量初始化 创建类对象时会被初始化
     * 构造方法 同上
     * Spring提供的功能
     * 1.实现BeanPostProcessor接口的postProcessBeforeInitialization，Bean初始化之前被调用，
     * 2.@PostConstruct注解的方法，依赖注入完成后，Bean 初始化过程中调用
     * 3.实现InitializingBean接口，afterPropertiesSet方法在 Bean 的属性设置完成后调用
     * 4.实现BeanPostProcessor接口的postProcessAfterInitialization方法，Bean初始化之后被调用
     * 4.实现ApplicationRunner接口，Spring 应用上下文启动完成后执行
     * 5.实现CommandLineRunner接口，Spring 应用上下文启动完成后执行
     */
    public String queryTopology(String ip) throws JsonProcessingException {
        Map<String, CloudService> cloudServiceMap = cmdbCacheService.getCloudService(ip);
        return mapper.writeValueAsString(cloudServiceMap);
    }
}
